<?php
// page_base.php
// 2005-2008 Derek "Kazan" Meek
// used by permission


class page
{	  
	
	var $app;
	var $title;
	
	// templates
	var $head_tpl = "page_templates/header.tpl";
	var $body_tpl = "page_templates/index.tpl";
	var $foot_tpl = "page_templates/footer.tpl";
  
  	function __construct(&$par)
  	{
  		$this->app =& $par;
  	}
  	 	
	function page_do()
	// do things that require authorization passed, etc
	// you should always call parent::page_do() when making child classes
	// failure to do so will break your page
	{		
		global $config;		
		
		$this->head = new template($this->head_tpl);
		$this->foot = new template($this->foot_tpl);
		$this->body = new template($this->body_tpl);
		
		@$this->head->assign_var('TITLE', $config['site_title'] . ": " . $this->title);
		@$this->head->assign_var('PGN', $_REQUEST['pgn']);
		
		$get = "";
		unset($_GET['pgn']);
		foreach ($_GET as $key=>$val)
		{
			$get .= "&$key=" . urlencode($val); 
		}
		
		// do menu
		$menu_class = $config['menu_type'];
		$menu = new $menu_class($this->app);
		if ($menu->load())
			@$this->head->assign_var('MENU', $menu->render());
		// done w/ menu
		
		@$this->head->assign_var('GET', $get);

		//@$this->head->assign_var('DEBUG', "<pre>" . print_r($_SESSION, true). "</pre>");
	}
  
	function pre_process()
	// do things which may result in page redirections, terminations, etc
	// when making your own ALWAYS call 
	//		if (!parent::pre_process()) return false;
	// before doing anything
	// if the user fails authorization return false;
	// failure to follow this instructions will break your page
	{
		if (@$_REQUEST['logout'] == 1)
		{
			$this->app->sess->reset_session();
			header("Location: index.php");
			return false;
		}
		
		return true;
	}
	
	function display($use_msg = false, $msg = "")
	{
		
		if (!empty($this->head)) echo $this->head->process();
		if (!$use_msg)
		{
			if (!empty($this->body)) echo $this->body->process();
		}
		else
		{
			echo $msg;
		}
			
		if (!empty($this->foot)) echo $this->foot->process();
	}
	  
}


class AuthenticatedPage extends page
{
	var $access_level = 1;
	
	function pre_process()
	// do things which may result in page redirections, terminations, etc
	// when making your own ALWAYS call 
	//		if (!parent::pre_process()) return false;
	// before doing anything
	// if the user fails authorization return false;
	// failure to follow this instructions will break your page
	{
		if (!parent::pre_process()) return false;
		
		if (!$this->app->sess->isLoggedIn() || $this->app->sess->access_level < $this->access_level)
		{
			header("Location: index.php?pgn=login");
			return false;
		}
		
		return true;
	}
}

class AjaxPage extends AuthenticatedPage
{
	var $output = "";
	
	function page_do()
	{
	
	}
	
	function display($use_msg = false, $msg = "")
	{
		// returns standard json replies - expect "success" and "msg" members of the return object
		// wraps it in a html <textarea> for dojoIFrame submits if the dojoIFrameIO get/post param is set to 1
		if (!$use_msg)
		{
			if (@$_REQUEST['dojoIFrameIO'] == 1)
				echo "<textarea>" . $this->output . "</textarea>";
			else
				echo $this->output;
		}
		else
		{
			if (@$_REQUEST['dojoIFrameIO'] == 1)
				echo "<textarea>{ \"success\": 0, \"msg\": \"" . addslashes($msg) . "\" }</textarea>";
			else
				echo "{ \"success\": 0, \"msg\": \"" . addslashes($msg) . "\" }";
		}
	}
}

?>